package com.zmn.oms.task.job.conf.distribute;

import com.github.ltsopensource.core.domain.Action;
import com.github.ltsopensource.core.domain.Job;
import com.github.ltsopensource.spring.tasktracker.JobRunnerItem;
import com.github.ltsopensource.spring.tasktracker.LTS;
import com.github.ltsopensource.tasktracker.Result;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.manager.lts.interfaces.task.TaskManager;
import com.zmn.oms.business.interfaces.conf.distribute.DistributeWeightPlatBService;
import com.zmn.oms.manager.utils.TaskConsts;
import lombok.extern.slf4j.Slf4j;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

/**
 * 类描述：工程师分级保护配置
 *
 * @author liuyingying
 * @since 2022/9/15 21:46
 */
@Slf4j
@LTS
public class ConfDistributeProtectionEngineerLevelJob {

    @Resource
    private TaskManager taskManager;
    @Resource
    private DistributeWeightPlatBService distributeWeightPlatBService;


    /**
     * 每天早上1点执行
     */
    @PostConstruct
    void init() {
        Job job = new Job();
        job.setTaskId(TaskConsts.TASK_ID_CONF_DISTRIBUTE_PROTECTION_ENGINEER_LEVEL_JOB);
        job.setParam(TaskManager.SHARD_FIELD, TaskConsts.TASK_ID_CONF_DISTRIBUTE_PROTECTION_ENGINEER_LEVEL_JOB);
        job.setCronExpression("0 0 3 * * 1");
        job.setReplaceOnExist(true);
        ResponseDTO<String> result = taskManager.submitTask2(job);
        if (!result.isSuccess()) {
            log.error("#ConfDistributeProtectionEngineerLevelJob#init job submit fail...");
        }
    }

    /**
     * 每周一早上3点执行
     *
     * @param job
     * @return
     */
    @JobRunnerItem(shardValue = TaskConsts.TASK_ID_CONF_DISTRIBUTE_PROTECTION_ENGINEER_LEVEL_JOB)
    public Result distributeProtectionEngineerLevelJob(Job job) {
        try {
            log.info("#ConfDistributeProtectionEngineerLevelJob#distributeProtectionEngineerLevelJob# 开始执行工程师分级保护配置任务。");
            distributeWeightPlatBService.saveEngineerLevelGroupToCurrent();
            log.info("#ConfDistributeProtectionEngineerLevelJob#distributeProtectionEngineerLevelJob# 开始执行工程师分级保护配置任务完成。");
            return new Result(Action.EXECUTE_SUCCESS, "执行成功了!");
        } catch (Exception ex) {
            log.error("#ConfDistributeProtectionEngineerLevelJob#distributeProtectionEngineerLevelJob# 任务执行失败：{}", ex);
            return new Result(Action.EXECUTE_FAILED, "工程师分级保护配置任务执行失败");
        }
    }


}
